using UnityEngine;
using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {
    internal class BlackboardEntryFieldPreview : VisualElement {

        private readonly AGEEnvironment ageEnv;
        private PreviewManager previewManager => ageEnv.ageContext.previewManager;

        Label variableName;

        // Label typeName;
        VisualElement c1;
        VisualElement c2;
        private IntegerField intField;
        private FloatField floatField;
        private Vector2Field vec2Field;

        public BlackboardEntryFieldPreview(AGEEnvironment ageEnv) {
            this.ageEnv = ageEnv;
            OnDrawUI();
        }

        public void SetData(AGABlackboardType bbType, string bbName) {
            variableName.text = bbName;
            // typeName.text = entry.type.ToString();
            DrawC2(bbType, bbName);
        }
        void OnDrawUI() {
            style.flexDirection = FlexDirection.Column;
            c1 = new VisualElement();
            c2 = new VisualElement();
            Add(c1);
            Add(c2);

            c1.style.flexDirection = FlexDirection.Row;
            variableName = new() {
                name = "name",
                style = {
                    unityTextAlign = TextAnchor.MiddleLeft,
                    marginTop = 1,
                    marginBottom = 1,
                    borderTopWidth = 1,
                    borderBottomWidth = 1,
                    paddingTop = 1,
                    paddingBottom = 1,
                    flexGrow = 1,
                }
            };

            // typeName = new() {
            //     name = "type",
            //     style = {
            //         unityTextAlign = TextAnchor.MiddleRight,
            //         marginTop = 1,
            //         marginBottom = 1,
            //         borderTopWidth = 1,
            //         borderBottomWidth = 1,
            //         paddingTop = 1,
            //         paddingBottom = 1,
            //         flexGrow = 1,
            //     }
            // };
            c1.Add(variableName);
            // c1.Add(typeName);

            c2.style.flexDirection = FlexDirection.Row;
        }

        void DrawC2(AGABlackboardType bbType, string bbName) {
            c2.Clear();
            intField = null;
            floatField = null;
            vec2Field = null;
            VisualElement f = null;
            switch (bbType) {
                case AGABlackboardType.Int:
                    intField = new IntegerField("int");
                    c2.Add(intField);
                    intField.value = previewManager.GetBBValue<int>(bbName);
                    intField.RegisterValueChangedCallback(IntValueChange);
                    f = intField;
                    break;
                case AGABlackboardType.Float:
                    floatField = new FloatField("float");
                    c2.Add(floatField);
                    floatField.value = previewManager.GetBBValue<float>(bbName);
                    floatField.RegisterValueChangedCallback(FloatValueChange);
                    f = floatField;
                    break;
                case AGABlackboardType.Vector2:
                    vec2Field = new Vector2Field("vector2");
                    c2.Add(vec2Field);
                    vec2Field.value = previewManager.GetBBValue<Vector2>(bbName);
                    vec2Field.RegisterValueChangedCallback(Vec2ValueChange);
                    f = vec2Field;
                    break;
            }
            if (f != null) {
                f.style.flexGrow = 1;
            }
        }
        private void Vec2ValueChange(ChangeEvent<Vector2> evt) {
            previewManager.SetBBValue(variableName.text, evt.newValue);
        }
        private void FloatValueChange(ChangeEvent<float> evt) {
            previewManager.SetBBValue(variableName.text, evt.newValue);
        }
        private void IntValueChange(ChangeEvent<int> evt) {
            previewManager.SetBBValue(variableName.text, evt.newValue);
        }

    }
}
